import {getGoodsList} from "../../service/goods";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:'',//搜索框的值
    itemTitle:'分类',//下拉菜单第二列的名称
    option1:[
      {text:'默认',value: 0},
      {text:'销量',value: 1},
      {text:'推荐',value: 2},
      {text:'价格',value: 3},
      {text:'评论',value: 4}
    ],
    value1:0,//下拉菜单第一类选项的默认值
    mainActiveIndex:0,
    activeId:null,
    items: [],
    goods:[],
    isLoading:false,
    isFirst:true,
    isLast:false,
    page:1,//分类
    sales:0,//选择销量的时候为1，否则为0
    recommend:0,//选择推荐的时候为1，否则为0
    price:0,//选择价格的时候为1，否则为0
    comments_count:0,//选择评论的时候为1，否则为0
    category_id:''
  },

  /**
   * 生命周期函数--监听页面 加载
   */
  onLoad: function (options) {
    /**
     * 获取数据
     * @type {{page}}
     */
    const params = {
      page: this.data.page,
      // category_id:this.data.category_id
    }
    this.getData(params)
  },
  /**
   * 获取数据
   * @param params
   */
  getData(params){
    this.setData({
      isLoading: true
    })


    /**
     * 获取商品列表
     */
    getGoodsList(params).then(res=>{
      // console.log(res)

      this.setData({
        isLoading: false
      })
      const {per_page} = res.goods
      /**
       * 修改name字段为text
       * @type {string}
       */
      const categories = JSON.stringify(res.categories)
      const item = categories.replace(/name/gi,'text')
      this.setData({
        items: JSON.parse(item),
      })

      if(this.data.isFirst){
        this.setData({
          isFirst: false
        })
      }
      this.setData({
        goods: [...this.data.goods,...res.goods.data]
      })
      if(res.goods.data<per_page){
        this.setData({
          isLast: true
        })
      }
    })
  },
  onChange(event){
    this.setData({
      value: event.detail
    })
  },
  /**
   * 搜索事件
   */
  onSearch(){
    this.setData({
      goods: [],
      title:this.data.value
    })
    const {sales,recommend,price,comments_count,title,page,category_id} = this.data
    const params = {sales,recommend,price,comments_count,title,page,category_id}
    // console.log(params)
    this.getData(params)
  },
  /**
   * 清空搜索框事件
   */
  onClear(){
    this.setData({
      goods: [],
      title:''
    })
    const {sales,recommend,price,comments_count,title,page,category_id} = this.data
    const params = {sales,recommend,price,comments_count,title,page,category_id}
    this.getData(params)
  },
  /**
   *下拉菜单事件
   * @param detail
   */
  Change(event){
    console.log(event.detail)
    if(event.detail == 0){
      this.setData({
        goods: [],
        sales:0,
        recommend:0,
        price:0,
        comments_count:0
      })
      if(this.data.title == undefined) this.data.title = ''
      const {sales,recommend,price,comments_count,title,page,category_id} = this.data
      const params = {sales,recommend,price,comments_count,title,page,category_id}
      this.getData(params)
    }
    if(event.detail == 1){
      this.setData({
        goods: [],
        sales:1,
        recommend:0,
        price:0,
        comments_count:0
      })
      if(this.data.title == undefined) this.data.title = ''
      const {sales,recommend,price,comments_count,title,page,category_id} = this.data
      const params = {sales,recommend,price,comments_count,title,page,category_id}
      this.getData(params)
    }
    if(event.detail == 2){
      this.setData({
        goods: [],
        sales:0,
        recommend:1,
        price:0,
        comments_count:0
      })
      if(this.data.title == undefined) this.data.title = ''
      const {sales,recommend,price,comments_count,title,page,category_id} = this.data
      const params = {sales,recommend,price,comments_count,title,page,category_id}
      this.getData(params)
    }
    if(event.detail == 3){
      this.setData({
        goods: [],
        sales:0,
        recommend:0,
        price:1,
        comments_count:0
      })
      if(this.data.title == undefined) this.data.title = ''
      const {sales,recommend,price,comments_count,title,page,category_id} = this.data
      const params = {sales,recommend,price,comments_count,title,page,category_id}
      this.getData(params)
    }
    if(event.detail == 4){
      this.setData({
        goods: [],
        sales:0,
        recommend:0,
        price:0,
        comments_count:1
      })
      if(this.data.title == undefined) this.data.title = ''
      const {sales,recommend,price,comments_count,title,page,category_id} = this.data
      const params = {sales,recommend,price,comments_count,title,page,category_id}
      this.getData(params)
    }
  }
  ,
  /**
   * 点击分类下拉框事件
   * @param detail
   */
  onClickNav({ detail = {} }) {
    // console.log(detail)
    this.setData({
      mainActiveIndex: detail.index || 0,
    });
  },
  /**
   * 分类下拉框内容事件
   * @param detail
   */
  onClickItem({ detail = {} }) {
    console.log(detail)
    this.setData({
      itemTitle: detail.text
    })
    const activeId = this.data.activeId === detail.id ? null : detail.id;

    this.setData({ activeId,goods:[] });
    if(this.data.title == undefined) this.data.title = ''
    if(activeId == null){
      this.setData({category_id:''})
      const {sales,recommend,price,comments_count,title,page} = this.data
      const params = {sales,recommend,price,comments_count,title,page}
      this.getData(params)
      this.setData({
        itemTitle: '分类'
      })
    }else{
      this.setData({category_id: activeId})
      const {sales,recommend,price,comments_count,title,page,category_id} = this.data
      const params = {sales,recommend,price,comments_count,title,page,category_id}
      this.getData(params)
    }
    /**
     * 实现点击后下拉框返回
     */
    this.selectComponent('#item').toggle()
  },

  /**
   * 触底加载更多
   */
  onReachBottom() {
    if(this.data.isLast) return
    this.setData({
      page: this.data.page + 1
    })
    const {sales,recommend,price,comments_count,title,page,category_id} = this.data
    const params = {sales,recommend,price,comments_count,title,page,category_id}
    this.getData(params)
  },
})